package com.bc004346.travelus;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.bc004346.travelus.Adapter.NoticesAdapter;
import com.bc004346.travelus.DataAccess.AsyncTasks.GetNoticesTaskFragment;
import com.bc004346.travelus.Model.Interface.IGetNoticesCallback;

import java.util.List;

import static com.bc004346.travelus.Utility.Utility.GET_NOTICES_TAG;
import static com.bc004346.travelus.Utility.Utility.log;
import static com.bc004346.travelus.Utility.Utility.onActivityStart;
import static com.bc004346.travelus.Utility.Utility.onActivityStop;

/**
 * Created by Renats on 5/31/2014.
 * Travel alerts from the profile page
 */
public class NoticesActivity extends Activity implements IGetNoticesCallback {

    private ProgressBar mProgressBar;

    @Override
    protected void onStart() {
        super.onStart();
        onActivityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        onActivityStop(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            setContentView(R.layout.activity_notices);
            Button button = (Button) findViewById(R.id.activity_notices_button_continue);
            mProgressBar = (ProgressBar) findViewById(R.id.activity_notices_progressBar);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showMyFlights();
                }
            });

            GetNoticesTaskFragment fragment =
                    (GetNoticesTaskFragment) getFragmentManager().findFragmentByTag(GET_NOTICES_TAG);
            if (fragment == null) {
                fragment = new GetNoticesTaskFragment();
                getFragmentManager().beginTransaction()
                        .add(fragment, GET_NOTICES_TAG).commitAllowingStateLoss();
            }

        } catch (Exception ex) {
            log(ex, this);
        }
    }

    private void showMyFlights() {
        Intent intent = new Intent(this, MyFlightListActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
        finish();
    }

    @Override
    public void onGetProfilePostExecute(List<String> result, Exception e) {

        // remove used fragment
        GetNoticesTaskFragment fragment =
                (GetNoticesTaskFragment) getFragmentManager().findFragmentByTag(GET_NOTICES_TAG);
        if (fragment != null)
            getFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();

        mProgressBar.setVisibility(View.INVISIBLE);
        ListView listView = (ListView) findViewById(R.id.activity_notices_notices);
        NoticesAdapter adapter =
                new NoticesAdapter(this, R.id.activity_notices_notices, result.toArray(new String[result.size()]));
        listView.setAdapter(adapter);
    }
}
